package com.sh.suhe.utildemos.canvas;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.sh.suhe.utildemos.R;

public class MyGradientView extends View {

    private Bitmap bitmap;
    private Paint paint;
    private BitmapShader bitmapShader;

    public MyGradientView(Context context) {
        super(context);
        paint = new Paint();
        bitmap = ((BitmapDrawable)getResources().getDrawable(R.mipmap.download)).getBitmap();
    }

    public MyGradientView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        bitmap = ((BitmapDrawable)getResources().getDrawable(R.mipmap.download)).getBitmap();
    }

    public MyGradientView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        bitmap = ((BitmapDrawable)getResources().getDrawable(R.mipmap.download)).getBitmap();
    }

    public MyGradientView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.TRANSPARENT);

        //canvas.drawBitmap(bitmap, 0, 0, paint);
        bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint.setShader(bitmapShader);
        canvas.drawRect(new Rect(0, 0, 900, 1000), paint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        int childCount =
    }
}
